package fzrj.qkl.demo.service.backend.impl;

import fzrj.qkl.demo.raw.Shipment;
import fzrj.qkl.demo.service.backend.UserBackendService;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

@Slf4j
@Service
public class IUserBackendService implements UserBackendService {

    // 通过系统配置文件获取到合约的地址值
    @Value("${system.contract.shipmentAddress}")
    private String contractAddress;

    // 获取到SDK交互的Client
    @Autowired
    private Client client;

    @Override
    public boolean addUser(String name) {

        Shipment shipment = Shipment.load(contractAddress,client,client.getCryptoSuite().getCryptoKeyPair());

        TransactionReceipt transactionReceipt = shipment.addUser(name);

        return transactionReceipt.isStatusOK();
    }

    @Override
    public boolean checkUser(int id) throws ContractException {

        // 1、 获取合约的实例
        Shipment shipment = Shipment.load(contractAddress,client,client.getCryptoSuite().getCryptoKeyPair());

        // 2、 与合约进行交互
        Boolean res = shipment.checkUser(new BigInteger(String.valueOf(id)));

        return res;
    }
}
